
public class Main {
    String[] map = { " ", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return new ArrayList<>();
        }
        dfs(digits, new StringBuilder(), 0);
        return res;
    }

    List<String> res = new ArrayList<>();

    void dfs(String digits, StringBuilder curStr, int index) {
        if (index == digits.length()) {
            res.add(curStr.toString());
            return;
        }
        char c = digits.charAt(index);
        int pos = c - '0';
        String map_string = map[pos];
        for (int i = 0; i < map_string.length(); i++) {
            curStr.append(map_string.charAt(i));
            dfs(digits, curStr, index + 1);
            curStr.deleteCharAt(curStr.length() - 1);
        }
    }
}

